<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
//$this->model('task')->test();
$return['status']=$this->member_status;
$tabs=array(
	array('op'=>'list','title'=>'全部用户'),
); 
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=$_RQ['mid'];
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['level_id']){
		$params['level_id']=$_RQ['level_id'];
		$return['params']['level_id']=$_RQ['level_id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_nongfee_member',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	$jian_feetype=$this->setting['jian_feetype']?:3;
	$jian_feetypetitle=$this->credit_type[$jian_feetype];
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			if($item['fromuser']){
				$item['agent']=cfc('core')->member_show($item['fromuser']);
				$item['agent']['nickname']=$item['agent']['nickname']?:'会员已删除';
				$item['agent']['nickname'] .='(ID:'.$item['fromuser'].')';
			}
			$credit=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','credit3','credit4','credit5','credit6','credit3_fz'));
			$item['credit']=$credit;
			$item['credit3']=$credit['credit3'];
			$item['credit3_fz']=$credit['credit3_fz'];
			$item['jian_feetype']=$credit['credit'.$jian_feetype];
			$team=$this->get('team',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			if($team['id']){
				$item['isteam']=1;
			}else{
				$item['isteam']=0;
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：</br>这里只显示应用的会员！','edit'=>false,'del_one'=>true,'delall'=>true,
		'total'=>$total,
		'psize'=>$psize,
		'list'=>array(
			array('name'=>'mid','title'=>'会员id','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'agent','title'=>'上级','type'=>'member'),
			array('name'=>'credit.credit1','title'=>'积分','type'=>'string'),
			array('name'=>'baofee','title'=>$this->db_title,'type'=>'string'),
			array('name'=>'jian_feetype','title'=>$jian_feetypetitle,'type'=>'string'),
			array('name'=>'ispay','title'=>'有效？','type'=>'radio','options'=>'ispay'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'chongzhi','title'=>'充值'.$this->db_title),
			array('type'=>'modal','op'=>'jian_feetype','title'=>'充值'.$jian_feetypetitle),
			array('type'=>'href','op'=>'change','title'=>'修改推荐人'),
			array('type'=>'modal','op'=>'setteam','title'=>'设置为团长','show'=>array('isteam'=>0)),
			//array('type'=>'modal','op'=>'set','title'=>'设置状态'),
		)),
		'search'=>array(
			//array('name'=>'level_id','width'=>15,'title'=>'分销等级','type'=>'select','options'=>'levels'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),

		)
	);
	$return['htmls']=$htmls;	
	$return['ispay']=array('无效会员(未支付)','有效会员');	
	exi($return);
}
if($_RQ['op']=='export'){
	//会员列表
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=$_RQ['mid'];
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['level_id']){
		$params['level_id']=$_RQ['level_id'];
		$return['params']['level_id']=$_RQ['level_id'];
	}
	$list=pdo_getall('jy_nongfee_member',$params,$fields);
	$jian_feetype=$this->setting['jian_feetype']?:3;
	$jian_feetypetitle=$this->credit_type[$jian_feetype];
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			if($item['fromuser']){
				$item['agent']=cfc('core')->member_show($item['fromuser']);
				$item['agentnickname']=$item['agent']['nickname']?:'会员已删除';
			}
			$item['username']=$item['member']['username'];
			$item['nickname']=$item['member']['nickname'];
			$item['mobile']=$item['member']['mobile'];
			$credit=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','credit3','credit4','credit5','credit6','credit3_fz'));
			$item['credit']=$credit;
			$item['credit1']=$credit['credit1'];
			$item['credit4']=$credit['credit4'];
			$item['credit3']=$credit['credit3'];
			$item['credit3_fz']=$credit['credit3_fz'];
			$item['jian_feetype']=$credit['credit'.$jian_feetype];
			$team=$this->get('team',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			if($team['id']){
				$item['isteam']=1;
			}else{
				$item['isteam']=0;
			}
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$credit3=$this->credit_type[3]?:'茶币';
	$credit4=$this->credit_type[4]?:'茶果';
	$header=array(
		'id'=>'会员ID',
		'username'=>'会员账号',
		'nickname'=>'会员昵称',
		'mobile'=>'手机号',
		'agentnickname'=>'上级账号昵称',
		'fromuser'=>'上级ID',
		'credit1'=>'账户积分',
		'credit3'=>'账户'.$credit3,
		'credit4'=>'账户'.$credit4,
		'baofee'=>'账户'.$this->db_title,
		'createtime'=>'注册时间',
	);
	model('core/tools')->export2excel($header,$list,'会员数据');
}
if($_RQ['op']=='chongzhi'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(intval($post['money'])<=0){
			exi('请先设置充值金额！并且为整数！','error');
		}
		$fee=sprintf("%.2f", intval($post['money']));
		$this->model('member')->recharge($post['mid'],$fee,0,'充值'.$this->db_title);
		exi('充值'.$this->db_title.'成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'手动充值'.$this->db_title,
		'edit'=>array(
			array('name'=>'tip','title'=>'<red>请谨慎操作</red>，后台手动充值'.$this->db_title.'，消息记录会产生充值记录!','type'=>'tip'),
			array('name'=>'money','title'=>'要充值的'.$this->db_title.'金额','type'=>'string'),
		),
	);
	exi($return);
}
if($_RQ['op']=='jian_feetype'){
	$jian_feetype=$this->setting['jian_feetype']?:3;
	$jian_feetypetitle=$this->credit_type[$jian_feetype];
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(intval($post['money'])<=0){
			exi('请先设置充值金额！并且为整数！','error');
		}
		$fee=sprintf("%.2f", intval($post['money']));
		model('core/member')->member_credit($post['mid'],'credit'.$jian_feetype,$fee,true,'jy_nongfee','手动充值');
		exi('充值'.$jian_feetypetitle.'成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'手动充值'.$jian_feetypetitle,
		'edit'=>array(
			array('name'=>'tip','title'=>'<red>请谨慎操作</red>，后台手动充值'.$jian_feetypetitle.'，消息记录会产生充值记录!','type'=>'tip'),
			array('name'=>'money','title'=>'要充值的'.$jian_feetypetitle.'金额','type'=>'string'),
		),
	);
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		$fromuser=intval($post['fromuser']);
		$this->model('member')->set_parent($mid,$fromuser,true);
		$this->model('team')->set_parent($mid,$fromuser,true);
		exi('修改推荐人成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])),array('mid','fromuser'));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改推荐人',
		'edit'=>array(
			array('type'=>'choose','name'=>'fromuser','title'=>'设置推荐人','action'=>'core/member.get_member/','help'=>'填写0表示设置为总店'),
		),
	);
	exi($return);
}
if($_RQ['op']=='setteam'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$mid=intval($post['mid']);
		if(intval($post['isteam'])){
			$team=$this->get('team',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			if(!$team['id']){
				$new_apply=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$mid,
						'status'=>1,
						'fromuser'=>$post['fromuser'],
						'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_nongfee_team',$new_apply);
			}
		}
		exi('操作成功！','','list');
	}
	$item=$this->get('member',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定会员不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'设置为团长',
		'edit'=>array(
			array('name'=>'isteam','title'=>'设置为团长？','type'=>'switch'),
		),
	);
	exi($return);
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_nongfee_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_nongfee_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}